local NetworkHandlerClass = class()
NetworkHandler = NetworkHandlerClass.new()

function NetworkHandlerClass:Init()
    NetworkManager.Instance.HandleNetMsg:AddCallback(self, self.HandleNetMsg)
    NetworkManager.Instance.HandleConnect:AddCallback(self, self.HandleConnect)
    NetworkManager.Instance.HandleDisConnect:AddCallback(self, self.HandleDisConnect)
end

function NetworkHandlerClass:HandleConnect(Type, ConnectTimes)
    print("已连接服务器", System.Enum.ToString(Type))
    Event.Brocast(EventType.UIWaitExit)
    if Type == ServerType.LoginServer then
        Event.Brocast(EventType.UILoginEnter)
    elseif Type == ServerType.GateServer then
        NetworkSender:AskLoginGate()
    end
end

function NetworkHandlerClass:HandleDisConnect(Type)
    print("已断开连接", System.Enum.ToString(Type))
    Event.Brocast(EventType.UIWaitEnter)
end

function NetworkHandlerClass:HandleNetMsg(Data, MsgID)
    print("接受消息", MsgID)
    if MsgID == GC_LS_pb.LS2GC_NotifyServerBSAddr then
        local recv = GC_LS_pb.ServerBSAddr()
        recv:ParseFromString(Data)
        self:OnGetServerList(recv)
    elseif MsgID == GC_GS_pb.GC2GS_TokenLogin then
        local login = GC_GS_pb.TokenLogin()
        login:ParseFromString(Data)
        self:EnterLobby(login)
    elseif MsgID == CS_GS_pb.CS2GS_UserInfo then
        local userInfo = CS_GS_pb.UserInfo()
        userInfo:ParseFromString(Data)
        Event.Brocast(EventType.UserInfo, userInfo)
    elseif MsgID == CS_GS_pb.CS2GS_StartMatch then

    elseif MsgID == CS_GS_pb.CS2GS_EnterRoom then
        local msgInfo = CS_GS_pb.EnterRoom()
        msgInfo:ParseFromString(Data)
        self:EnterRoom(msgInfo)
    elseif MsgID == CS_GS_pb.CS2GS_Map then
        local msgInfo = CS_GS_pb.Map()
        msgInfo:ParseFromString(Data)
        self:CreateMap(msgInfo)
    elseif MsgID == CS_GS_pb.CS2GS_Transform then
        local msgInfo = CS_GS_pb.Transform()
        msgInfo:ParseFromString(Data)
        self:TransformChange(msgInfo)
    elseif MsgID == CS_GS_pb.CS2GS_UpdateCells then
        local msgInfo = CS_GS_pb.UpdateCells()
        msgInfo:ParseFromString(Data)
        self:UpdateCells(msgInfo)
    elseif MsgID == CS_GS_pb.CS2GS_ChangeName then
        local msgInfo = CS_GS_pb.ChangeName()
        msgInfo:ParseFromString(Data)
        self:ChangeName(msgInfo)
    elseif MsgID == GC_LS_pb.LS2GC_Error then
        local recv = GC_LS_pb.ErrorMsg()
        recv:ParseFromString(Data)
        self:HandleError(recv.errorid)
    elseif MsgID == GC_GS_pb.GS2GC_Error then
        local recv = GC_GS_pb.ErrorMsg()
        recv:ParseFromString(Data)
        self:HandleError(recv.errorid)
    end
end

function NetworkHandlerClass:HandleError(Error)
    local errorInfo = ErrorID[tonumber(Error)]
    if errorInfo then
        print(errorInfo)
    else
        print("无法处理的错误ID", Error)
    end
end

function NetworkHandlerClass:OnGetServerList(Msg)
    local ServerList = {}
    for key, value in ipairs(Msg.serverinfo) do
        local server = {}
        server.Name = value.ServerName
        server.Addr = value.ServerAddr
        server.Port = value.ServerPort
        server.State = value.ServerState
        table.insert(ServerList, server)
    end
    Event.Brocast(EventType.UISelectServerEnter, ServerList)
    Event.Brocast(EventType.UILoginExit)
end

function NetworkHandlerClass:EnterLobby(Msg)
    local player = EntityManager:GetPlayer()
    player.Token = Msg.token
    Event.Brocast(EventType.UISelectServerExit)
    local loadFinished = function()
        WindowManager:ReGetCanvas()
        Event.Brocast(EventType.UILobbyEnter)
        Event.Brocast(EventType.UILogEnter)
    end
    LoadingScreen.LoadScene("Lobby", loadFinished)
end

function NetworkHandlerClass:EnterRoom(Msg)
    local mail = Msg.player.mail
    local player = EntityManager:GetEntity(mail)
    if player == nil then
        player = EntityManager:CreateEntity(mail)
    end
    player.Mail = mail
    player.RoomID = Msg.player.roomid
    player.Name = Msg.player.name
    player.Score = Msg.player.score
    player.CellTypeID = Msg.player.celltypeid
    player.CurrentCell = Map:GetCell(Msg.player.currentCell)

    player:CreateCharacter()

    player.Manors = Map:SetManors(Msg.player.manors)
    player.Tracks = Map:SetTracks(Msg.player.tracks)
end

function NetworkHandlerClass:CreateMap(Msg)
    Map:CreateMap(Msg)
    Map:JenerateMap()
    EntityManager:CreateEntitys(Msg.players)
    
    local loadFinished = function()
        WindowManager:ReGetCanvas()
        Event.Brocast(EventType.UILogEnter)
        Event.Brocast(EventType.UIRoomEnter)
        Event.Brocast(EventType.RoomCreated)
    end
    LoadingScreen.LoadScene("Battle01", loadFinished)
end

function NetworkHandlerClass:TransformChange(Msg)
    EntityManager:TransformChange(Msg)
end

function NetworkHandlerClass:UpdateCells(Msg)
    Map:UpdateCells(Msg)
end

function NetworkHandlerClass:ChangeName(Msg)
    local result = Msg.result
    if result == 1 then
        local mail = Msg.mail
        local name = Msg.name
        local player = EntityManager:GetEntity(mail)
        if player then
            player.Name = name
            Event.Brocast(EventType.NameChange, player)
        end
    end
end
